import com.sun.org.apache.bcel.internal.generic.SWAP;

import java.util.Arrays;
import java.util.*;
public class Array {

    public static void main1(String[] args) {
        int[] array = new int[]{1,2,5,8,9,6,3};
        System.out.println(Arrays.toString(array));

    }

    public static void main2(String[] args) {
        int[] luckNumber ={1,4,5,8,9,6,3,2};
        //int[] copyNumber = Arrays.copyOf(luckNumber,luckNumber.length);
        Arrays.sort(luckNumber);
        System.out.println(Arrays.toString(luckNumber));
    }


    public static void main3(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("How many numbers do you need to draw? ");//你要抽取多少数字？
        int k = in.nextInt();//输入

        System.out.print("What is the highest number you can draw? ");//你要抽取数字中最大的数字是多少？
        int n = in.nextInt();//输入

// fill an array with numbers 1 2 3 . . . n
        int[] numbers = new int[n];//产生1-highest的数字
        for (int i = 0; i < numbers.length; i++)
            numbers[i] = i + 1;//将元素分别放在数组中
// draw k numbers and put them into a second array
        int[] result = new int[k];//抽取的数字放在result数组中
        for (int i = 0; i < result.length; i++) {
// make a random index between 0 and n - 1
            int r = (int) (Math.random() *(n+1));//产生0-n-1的随机数
// pick the element at the random location
            result[i] = numbers[r];//将元素随机的下标放在抽取的数组里
// move the last element into the random location//最后的元素放在随机位置
            numbers[r] = numbers[n - 1];
            n--;
        }
// print the sorted array
        Arrays.sort(result);
        System.out.println("Bet the following combination. It'll make you rich!");
        for (int x : result)
            System.out.println(x);
    }

    public static void main4(String[] args) {
        Scanner scan =new Scanner(System.in);
        System.out.println("你要抽取多少个数字？:>");
        int k= scan.nextInt();
        System.out.println("你要抽取的数字中最大为多少?:>");
        int n = scan.nextInt();

        int[] numbers =new int[n];//创建一个n大小的数组
        int[] result =new int[k];//将抽取的数字放在result这个数组中。

        for(int i=0;i< numbers.length; ++i) {
            numbers[i]=i+1;//将1-n的数字放进numbers数组里
        }
        for(int j=0;j< result.length;++j) {
            int rand = (int) (Math.random()* n);//产生0-n-1的随机数

            result[j] =numbers[rand];
            numbers[rand] =numbers[n-1];
            n--;
        }

        Arrays.sort(result);
        System.out.println("你已经成功抽取，祝你好运!!!");
        for(int x:result) {
            System.out.print(x+" ");
        }
    }

    public static void main5(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入你要抽取多少个数字?:>");
        int k =scan.nextInt();
        System.out.println("请输入你抽取的数字中最大为多少?:>");
        int n =scan.nextInt();

        int[] numbers = new int[n];//将number设置为n长度
        int[] result =new int[k];//将抽取好的数字放进result数组中

        for(int i=0;i< numbers.length;++i) {
            numbers[i]=i+1;//将numbers数组中初始化为1-n的数字
        }
        for(int j=0;j< result.length;++j) {
            int rand = (int)(Math.random()*n);//产生0-n-1个下标
            result[j] = numbers[rand];//将随机数字的下标的数字放进result数组里这就睡抽取好的数字

            numbers[rand] =numbers[n-1];//将numbers数组中最后一个元素变更为随机的数字，防止与上次重复
            n--;
        }
        Arrays.sort(result);//将抽取好的数字进行排序
        for(int x: result) {
            System.out.print(x+" ");
        }
    }

        public static void main6(String[] args) {
            final int NMAX = 10;
                // allocate triangular array
            int[][] odds = new int[NMAX + 1][];
            for (int n = 0; n <= NMAX; n++)
                odds[n] = new int[n + 1];
                // fill triangular array
            for (int n = 0; n < odds.length; n++) {
                for (int k = 0; k < odds[n].length; k++) {
                    /*
                     * compute binomial coefficient n*(n-l)*(n-2)*...*(n-k+l)/(l*2*3*...*k)
                     */
                    int lotteryOdds = 1;
                    for (int i = 1; i <= k; i++)
                        lotteryOdds = lotteryOdds * (n - i + 1) / i;
                    odds[n][k] = lotteryOdds;
                    // print triangular array
                    for (int[] row : odds) {
                        for (int odd : row)
                            System.out.printf("%4d", odd);
                        System.out.println();
                    }
                }
            }
        }

    /**
     * Arrays工具类讲解
     * @param args
     */
    public static void main7(String[] args) {
             for(int i=0;i<args.length;++i) {
                 System.out.println(args[i]);
             }
        }

        public static int[] copyArray(int[] array) {
               int[] array2= new int[array.length];
               for(int i=0;i<array.length;++i) {
                   array2[i]=array[i];
               }
               return array2;
        }
        public static void main8(String[] args) {
            int[] array ={1,4,5,6,8};
            int[] ret =copyArray(array);
            System.out.println(Arrays.toString(ret));
        }

    public static void main9(String[] args) {
        int[] array = new int[]{1,4,7,8,9,6,3};
        int[] ret =new int[array.length];
        System.arraycopy(array,0,ret,0,array.length);
        System.out.println(Arrays.toString(ret));
    }

    public static void main10(String[] args) {
        int[] array = new int[]{1,4,7,8,9,6,3};
        int[] ret = array.clone();
        System.out.println(Arrays.toString(ret));
    }

    public static int arrayMaxNum(int[] arrays) {
        int max =arrays[0];
        for(int i=0;i<arrays.length;++i) {
            if(arrays[i]>max){
                max=arrays[i];
            }
        }
        return max;
    }
    public static void main11(String[] args) {
        //写一个方法找出一个数组中的最大值
        int[] arrays ={1,8,5,9,6,3,7,0};
        System.out.print("数组中的最大值为：");
        System.out.println(arrayMaxNum(arrays));
    }

    public static int binarySearch(int[] array,int key) {
        int left = 0;
        int right =array.length-1;
        while(left<=right) {
            int mid =(left+right)>>>1;
            if(array[mid]>key) {
                right =mid-1;
            }else if(array[mid]<key) {
                left =mid+1;
            }else {
                return mid;
            }
        }
        return -1;
    }
    public static void main12(String[] args) {
        int[] array ={1,5,7,8,9,10,15};
        //int find =binarySearch(array,15);
        int find =Arrays.binarySearch(array,5);
        System.out.println(find);
    }

    public static void main13(String[] args) {
        int[] array1 ={1,2,3,4,5,6,7,8,9};
        int[] array2 ={1,2,3,4,5,6,7,8,9};
        System.out.println(Arrays.equals(array1, array2));
    }

    public static void main14(String[] args) {
        int[] array=new int[10];
        Arrays.fill(array,2,5,1);
        System.out.println(Arrays.toString(array));
    }

    /**
     * 判断一个数组是否是有序的返回值是boolean类型的
     * 是有序的返回: true
     * 不是返回的是：false
     * @param args
     */

    public static boolean isUp(int[] array) {
        for(int i=1;i<array.length;++i) {
            if(array[i-1]>array[i]) {
                return false;
            }
        }
        return true;
    }
    public static void main15(String[] args) {
        int[] array ={1,2,3,6,5,4,9};
        System.out.println(isUp(array));
    }

    public static void Print(int[] array) {
        for(int i=0;i<array.length;++i) {
            System.out.print(array[i]+" ");
        }
    }

    public static void bubbleSort(int[] array) {
        for(int i=0;i<array.length;++i) {
            boolean flag =false;
            for(int j=0;j<array.length-1-i;++j) {
                if(array[j]>array[j+1]){
                    flag=true;
                    int tmp =array[j];
                    array[j]=array[j+1];
                    array[j+1]=tmp;
                }
            }
            if(flag==false) {
                break;
            }
        }
    }

    public static void reverseSort(int[] array) {
        int left =0;
        int right =array.length-1;;
        while(left< right) {
            int tmp =array[left];
            array[left]=array[right];
            array[right]=tmp;
            left++;
            right--;
        }
    }
    public static void main17(String[] args) {
        int[] array={1,2,3,4,5};
       // bubbleSort(array);
        reverseSort(array);
        System.out.println(Arrays.toString(array));
        //Print(array);
       // System.out.println(Arrays.toString(array));
    }

    /**
     * 实现一个方法进行就交换，数组前面放偶数，后面放奇数
     * 思路:当前面遇见奇数停止，当后面遇见偶数停止，然后进行交换
     * @param args
     */

    public static void parityExchange(int[] array) {
        int left =0;
        int right =array.length-1;
        while(left<right) {
            while(left<right&&array[left]%2==0) {
                left++;
            }
            while(left<right&&array[right]%2==1) {
                right--;
            }
            //进行奇数偶数交换
            if(left<right) {
                int tmp =array[left];
                array[left]=array[right];
                array[right]=tmp;
            }
        }
    }
    public static void main18(String[] args) {
        int[] array={8,9,7,5,6,3,3,0,2,1,0,8,5,4,3,0};
        parityExchange(array);
        System.out.println(Arrays.toString(array));
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
    }

    /**
     * 定义二维数组的方法
     * @param args
     */
    public static void main19(String[] args) {
        int[][] array ={{1,2,3},{3,4,5}};
//        int[][] array1 =new int[4][6];
//        int[][] array2= new int[][]{{1,2,3,},{4,5,6}};
        for (int i = 0; i <2; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.print(array[i][j]+" ");
            }
            System.out.println();
        }
    }

    public static void main20(String[] args) {
        int[][] array ={{1,2,3,4},{5,6,7,8}};
//        for (int i = 0; i < array.length; i++) {
//            for (int j = 0; j < array[i].length; j++) {
//                System.out.print(array[i][j]+" ");
//            }
//            System.out.println();
//        }

        System.out.println(Arrays.deepToString(array));
    }

    /**
     * 打印二维数组
     * @param args
     */
    public static void main21(String[] args) {
        int[][] array={{1,2,3,4},{5,6,7,8}};
        for(int[] tmp:array) {
            for(int x: tmp) {
                System.out.println(Arrays.toString(tmp));
            }
        }
    }

    /**
     * 实现不规则二维数组
     * @param args
     */
    public static void main(String[] args) {
        //int[][] array ={{1,2,3},{4,5,6}};
        int[][] array =new int[2][];
        array[0]=new int[]{1,2,3};
        array[1]=new int[]{4,5,6,7,8,9};
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j]+" ");
            }
            System.out.println();
        }
    }


}
